Imports System.Windows.Forms
Imports Vintasoft.Imaging.Codecs.Tiff

Public Partial Class TagAddForm
	Inherits Form

	#Region "Constructor"

	Public Sub New()
		InitializeComponent()
	End Sub

	#End Region


	#Region "Properties"

	Private _tagId As UShort = 58000
	Public ReadOnly Property TagId() As UShort
		Get
			Return _tagId
		End Get
	End Property

	Private _tagDataType As TiffTagDataType = TiffTagDataType.Ascii
	Public ReadOnly Property TagDataType() As TiffTagDataType
		Get
			Return _tagDataType
		End Get
	End Property

	Public ReadOnly Property StringValue() As String
		Get
			Return stringValueTextBox.Text
		End Get
	End Property

	Public ReadOnly Property IntegerValue() As Integer
		Get
			Return CInt(Math.Truncate(numInteger.Value))
		End Get
	End Property

	Public ReadOnly Property DoubleValue() As Double
		Get
			Dim value As Double
			If Double.TryParse(textDoubleValue.Text, value) Then
				Return value
			Else
				Return 0.0
			End If
		End Get
	End Property

	Public ReadOnly Property NumeratorValue() As Integer
		Get
			Return CInt(Math.Truncate(numNumerator.Value))
		End Get
	End Property

	Public ReadOnly Property DenominatorValue() As Integer
		Get
			Return CInt(Math.Truncate(numDenominator.Value))
		End Get
	End Property

	#End Region


	#Region "Methods"

	Private Sub bOk_Click(sender As Object, e As EventArgs)
		Try
			If [Enum].IsDefined(GetType(ReadOnlyTiffTagId), DirectCast(_tagId, ReadOnlyTiffTagId)) Then
				MessageBox.Show("Tag with ID from ReadOnlyTiffTagId enumeration cannot be added/updated.", "Add tag")
				Return
			End If
		Catch generatedExceptionName As ArgumentException
		End Try

		If _tagDataType = TiffTagDataType.Float OrElse _tagDataType = TiffTagDataType.[Double] Then
			Dim value As Double
			If Not Double.TryParse(textDoubleValue.Text, value) Then
				MessageBox.Show("Double value is incorrect!", "Add tag")
				Return
			End If
		End If

		DialogResult = System.Windows.Forms.DialogResult.OK
	End Sub

	Private Sub bCancel_Click(sender As Object, e As EventArgs)
		DialogResult = System.Windows.Forms.DialogResult.Cancel
	End Sub

	Private Sub comboTagDataType_SelectedIndexChanged(sender As Object, e As EventArgs)
		Select Case comboTagDataType.SelectedIndex
			Case 0
				_tagDataType = TiffTagDataType.Ascii
				Exit Select
			Case 1
				_tagDataType = TiffTagDataType.SShort
				Exit Select
			Case 2
				_tagDataType = TiffTagDataType.[Short]
				Exit Select
			Case 3
				_tagDataType = TiffTagDataType.SLong
				Exit Select
			Case 4
				_tagDataType = TiffTagDataType.[Long]
				Exit Select
			Case 5
				_tagDataType = TiffTagDataType.Float
				Exit Select
			Case 6
				_tagDataType = TiffTagDataType.[Double]
				Exit Select
			Case 7
				_tagDataType = TiffTagDataType.Rational
				Exit Select
			Case 8
				_tagDataType = TiffTagDataType.SRational
				Exit Select
		End Select
		Select Case comboTagDataType.SelectedIndex
			Case 0
				groupStringValue.Visible = True
				groupIntegerValue.Visible = False
				groupDoubleValue.Visible = False
				groupRationalValue.Visible = False
				Exit Select

			Case 1, 2, 3, 4
				groupStringValue.Visible = False
				groupIntegerValue.Visible = True
				groupDoubleValue.Visible = False
				groupRationalValue.Visible = False
				Exit Select

			Case 5, 6
				groupStringValue.Visible = False
				groupIntegerValue.Visible = False
				groupDoubleValue.Visible = True
				groupRationalValue.Visible = False
				Exit Select

			Case 7, 8
				groupStringValue.Visible = False
				groupIntegerValue.Visible = False
				groupDoubleValue.Visible = False
				groupRationalValue.Visible = True
				Exit Select
		End Select
		Invalidate()
	End Sub

	Private Sub numTagId_ValueChanged(sender As Object, e As EventArgs)
		_tagId = CUShort(Math.Truncate(numTagId.Value))
	End Sub

	#End Region

End Class
